package com.qianji.service.impl;

import com.qianji.mapper.PVUVLeaveMapper;
import com.qianji.mapper.PVUVMapper;
import com.qianji.pojo.PVUVDto;
import com.qianji.service.PVUVLeaveService;
import com.qianji.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class PVUVLeaveServiceImpl implements PVUVLeaveService {

    @Autowired
    private PVUVLeaveMapper pvuvMapper;

    @Override
    public Result addPV(PVUVDto pvuvDto) {
        Result result = new Result();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date =simpleDateFormat.format(new Date());
        PVUVDto list = pvuvMapper.findPV(pvuvDto.getLinkId(),pvuvDto.getImei(),date);
        if(list == null){
            pvuvDto.setCreateTime(new Date());
            pvuvDto.setUpdateTime(new Date());
            Integer status = pvuvMapper.addPV(pvuvDto);
            if(status == 1){
                result.setCode(200);
                result.setMessage("新增成功");
                result.setData(pvuvDto);
            }else {
                result.setCode(500);
                result.setMessage("新增失败");
                result.setData(status);
            }
        }else {
            result.setCode(200);
            result.setMessage("该用户IMEI已存在");
            result.setData(list);
        }

        return result;
    }

    @Override
    public Result addUV(PVUVDto pvuvDto) {
        Result result = new Result();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date =simpleDateFormat.format(new Date());
        PVUVDto list = pvuvMapper.findUV(pvuvDto.getLinkId(),pvuvDto.getImei(),date);
        if(list == null){
            pvuvDto.setCreateTime(new Date());
            pvuvDto.setUpdateTime(new Date());
            Integer status = pvuvMapper.addUV(pvuvDto);
            if(status == 1){
                result.setCode(200);
                result.setMessage("新增成功");
                result.setData(pvuvDto);
            }else {
                result.setCode(500);
                result.setMessage("新增失败");
                result.setData(status);
            }
        }else {
            result.setCode(200);
            result.setMessage("该用户IMEI已存在");
            result.setData(list);
        }

        return result;
    }

}
